#include "speed.h"

// pwm控制小车马达速度
void PWM12_Init(void)
{	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	//初始化GPIO口 PA6 PA7 用于产生PWM信号
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO A6 A7 
	
	TIM_InternalClockConfig(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //定时器不分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;//自动重装值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 36 - 1;//预分频值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//选择PWM模式1
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出比较极性选择
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能

	TIM_OC1Init(TIM3, &TIM_OCInitStructure);//初始化 TIM3 OC1
	TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能CCR1自动重装
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);//初始化 TIM3 OC2	
	TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能CCR2自动重装
	
	TIM_ARRPreloadConfig(TIM3,ENABLE);//开启预装载
	TIM_Cmd(TIM3, ENABLE);//开启定时器3
	TIM3->CCR1 = 0;//设置输出比较值
	TIM3->CCR2 = 0;
}

//设置PWM1比较值 为Compare 即输出比较值
void PWM12_SetCompare1(uint16_t Compare)
{
	TIM_SetCompare1(TIM3, Compare);
}

//设置PWM2比较值 为Compare
void PWM12_SetCompare2(uint16_t Compare)
{
	TIM_SetCompare2(TIM3, Compare);
}
